package com.xxc.hs.funActivity.panel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xxc.hs.R;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.QuestionViewHolder> {

    private List<Question> questionList;
    private Context context;
    private OnItemClickListener onItemClickListener;

    public QuestionAdapter(List<Question> questionList, Context context) {
        this.questionList = questionList;
        this.context = context;
    }

    @NonNull
    @Override
    public QuestionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_question, parent, false);
        return new QuestionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull QuestionViewHolder holder, int position) {
        Question question = questionList.get(position);
        holder.tvQuestionTitle.setText(question.getTitle());
        holder.tvQuestionDescription.setText(question.getDescription());

        Glide.with(context)
                .load(question.getImageResId())
                .placeholder(R.drawable.ic_launcher_foreground)
                .error(R.drawable.ic_launcher_foreground)
                .into(holder.iv_question_pic);
    }

    @Override
    public int getItemCount() {
        return questionList.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class QuestionViewHolder extends RecyclerView.ViewHolder {
        TextView tvQuestionTitle, tvQuestionDescription;
        ImageView iv_question_pic;

        public QuestionViewHolder(@NonNull View itemView) {
            super(itemView);
            tvQuestionTitle = itemView.findViewById(R.id.tv_question_title);
            tvQuestionDescription = itemView.findViewById(R.id.tv_question_description);
            iv_question_pic = itemView.findViewById(R.id.iv_question_pic);


            iv_question_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        Question question = questionList.get(position);
                        Intent intent = new Intent(context, ImagePreviewActivity.class);
                        intent.putExtra(ImagePreviewActivity.EXTRA_IMAGE_RES_ID, question.getImageResId());
                        context.startActivity(intent);
                        ((Activity) context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                    }
                }
            });

            // 设置项点击事件
            itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(questionList.get(getAdapterPosition()));
                }
            });


        }
    }

    public interface OnItemClickListener {
        void onItemClick(Question question);
    }
}